<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\command;

use app\enum\ConfigEnum;
use app\enum\OceanEnum;
use app\service\fetch\OceanFetchService;
use think\console\Input;
use think\console\Output;
use think\facade\Cache;

class OeSourceVideoWork extends BaseWork
{
    public const REDIS_TASK_KEY = ConfigEnum::CACHE_CONFIG[OceanEnum::LIST_CONFIG_KEY]['source_video'];

    protected function configure()
    {
        // 指令配置
        $this->setName('oe_source_video_work');
        // 设置参数
        $this->setDescription('拉取巨量直达链接视频');
    }

    protected function execute(Input $input, Output $output)
    {
        set_time_limit(0);
        $this->getOutputMessage('巨量引擎-拉取巨量直达链接视频开始');
        try {
            $redis = Cache::store('redis')->handler();
            $st = time() + 5 * 60;
            while (time() < $st) {
                $key = $redis->lPop(self::REDIS_TASK_KEY);
                if (! $key) {
                    sleep(rand(5, 10));
                    continue;
                }
                $this->getOutputMessage('巨量引擎-拉取巨量直达链接视频开始,key:' . $key);
                $res = OceanFetchService::fetchSourceVideo($key);
                if (! $res) {
                    $this->getOutputMessage('巨量引擎-拉取巨量直达链接视频失败,key:' . $key . ' err:' . OceanFetchService::getError(0));
                } else {
                    $this->getOutputMessage('巨量引擎-拉取巨量直达链接视频成功,key:' . $key);
                }
            }
        } catch (\Exception $e) {
            echo $e->getMessage() . PHP_EOL;
            echo $e->getFile() . '[' . $e->getLine() . ']' . PHP_EOL;
            echo $e->getTraceAsString() . PHP_EOL;
        }
        $this->getOutputMessage('巨量引擎-拉取巨量图片素材列表结束');
    }
}
